package com.cip.admin.dao.impl;

import java.util.List;

import com.cip.admin.dao.FriendsDAO;
import com.cip.admin.model.Friends;
import com.cip.admin.model.User;

public class FriendsDAOImpl extends CipHibernateDAOSupport implements FriendsDAO {
	
	public Friends saveOrUpdateFriends(Friends friends) throws Exception{
		getHibernateTemplate().saveOrUpdate( friends);
		return friends;
	}
	
	public List<User> getAllFriendsById(int requestFriendId) throws Exception{
		String query="select friends.responseFriend from Friends as friends where friends.status='Active' and friends.requestFriend='"+requestFriendId+"'";
		List<User> users = getCipHibernateTemplate().find(query);
		return users;
	}
	
	public List<User> getAllInactiveFriendsById(int requestFriendId) throws Exception{
		String query="select friends.responseFriend from Friends as friends where friends.status='inActive' and friends.requestFriend='"+requestFriendId+"'";
		List<User> users = getCipHibernateTemplate().find(query);
		return users;
	}
	
	public List<User> getAllUnConfirmedFriendsById(int responseFriendId) throws Exception{
		String query="select friends.requestFriend from Friends as friends where friends.status='inActive' and friends.responseFriend='"+responseFriendId+"'";
		List<User> users = getCipHibernateTemplate().find(query);
		return users;
	}
	
	public List<User> getSuggestedFriendsList(User requestFriend) throws Exception{
		String query="select user from User as user where user.postalCode='"+requestFriend.getPostalCode()+"'"
				+" or user.classYear='"+requestFriend.getClassYear()+"'"+" or user.section='"+requestFriend.getSection()+"'";
		List<User> users = getCipHibernateTemplate().find(query);
		return users;	
	}
	
	public List<User> getFriendBySearchName(String name) throws Exception{
		User user = null;
		String query="select user from User as user where user.firstName like '%"+name+"%'"
				+" or user.lastName like '%"+name+"%'"+" or user.username like '%"+name+"%'"
				+" or user.email like '%"+name+"%'";
		System.out.println("query===>"+query);
		List<User> users = getCipHibernateTemplate().find(query);
		return users;		
	}
	
	public Friends getFriendByRequestAndResponse(int requestFriendId, int responseFriendId) throws Exception{
		String query="select friends from Friends as friends where friends.requestFriend='"+requestFriendId+"'"
				+" and friends.responseFriend='"+responseFriendId+"'";
		List<Friends> friends = getCipHibernateTemplate().find(query);
		return friends.get(0);
	}

}
